﻿#include "IM_Account.h"
#include "IM_FriendMap.h"
#include "IM_VirtualData.h"
#include <cassert>
#include "IM_Agent.h"
#include "IM_DataInterface.h"

void IM_Account::_init_friend_map()
{
	m_friend_map = new IM_FriendMap(m_account_number,m_data_interface);
	assert(m_friend_map);

}

void IM_Account::print_infor()
{
#ifdef _DEBUG

	printf("\nnumber:%s\n\
			name:%s\n\
			agent_id:%d", m_account_number.c_str(), m_name.c_str(),m_agent?m_agent->get_context_handle():-1);


#endif // _DEBUG

}

void IM_Account::print_friends_infor()
{
	m_friend_map->print_all_frined();
}

IM_Account::IM_Account(const std::string& ac_number, const std::string & name, const std::string & emil, char sex,IM_DataInterface * data_interface)
	:m_account_number(ac_number),m_emil(emil),m_sex(sex),m_name(name)
	,m_agent(nullptr),m_friend_map(nullptr),m_data_interface(data_interface)
{
	
}

IM_Account::IM_Account(std::string&& ac_number, std::string && name, std::string && emil, char sex, IM_DataInterface * data_interface)
	:m_account_number(std::forward<std::string >(ac_number))
	, m_emil(std::forward<std::string >(emil))
	, m_sex(sex), m_name(std::forward<std::string >(name))
	, m_agent(nullptr)
	, m_friend_map(nullptr)
	,m_data_interface(data_interface)
{

}

void IM_Account::set_account_state(AccountState state)
{
	bool c = false;
	{
		std::lock_guard<std::mutex>{m_mutex_state};
		if (m_curr_state != state)
		{
			m_curr_state = state;
			c = true;
		}
	}
	if (c)
	{
		_state_change();
	}
}

IM_FriendMap * IM_Account::get_friend_map()
{
	return m_friend_map;
}

const std::string& IM_Account::get_name()
{
	return m_name;
}

const std::string& IM_Account::get_emil()
{
	return m_emil;
}

char IM_Account::get_sex()
{
	return m_sex;
}

IM_Account::~IM_Account()
{
}

void IM_Account::_state_change()
{
	if (m_curr_state==AccountState::on_line)
	{
		DEBUG("一位用户登陆成功\n");
		//初始化 好友映射 
		_init_friend_map();
		//调试
#ifdef _DEBUG
		print_friends_infor();
#endif // _DEBUG
	}
	else
	{
		DEBUG("一位用户退出登陆 \n");
		_relace_friend_map();
	}
}

void IM_Account::_relace_friend_map()
{
	delete m_friend_map;
	m_friend_map = nullptr;
	DEBUG("释放好友映射\n");
}

IM_AccountServer::IM_AccountServer()
{
	m_data_interface = IM_VirtualData::getInstance();
}

IM_AccountServer::~IM_AccountServer()
{

}


bool IM_AccountServer::init()
{
	return m_data_interface->load_ac_all(m_ac_map);
}

/*
bool IM_AccountServer::initAcFriendMap(call_get_friends get_friends_func)
{
	for (auto & v:m_ac_map)
	{
		auto friends = get_friends_func(v.first);
		IM_FriendMap * fm = new IM_FriendMap();
		fm->init(friends);
	}
}
*/
void IM_AccountServer::clear()
{
	for (auto v:m_ac_map)
	{
		delete v.second;
	}
	m_ac_map.clear();
}


IM_Account * IM_AccountServer::findAccount(const std::string& number)
{
	IM_Account * r = nullptr;
	m_mutex.lock();
	auto f = m_ac_map.find(number);
	if (f!=m_ac_map.end())
	{
		r = (*f).second;
	}
	m_mutex.unlock();
	return r;
}

bool IM_AccountServer::addAccount(const std::string &number, IM_Account* ac, bool overrides/*=false*/)
{
	m_mutex.lock();
	auto f = m_ac_map.find(number);
	if (f != m_ac_map.end())
	{

		DEBUG("正在覆盖原先的数据！\n");

		if (!overrides)
		{
			m_mutex.unlock();
			return false;
		}

		delete (*f).second;
		m_ac_map.erase(f);
	}
	m_ac_map.emplace(number, ac);
	m_mutex.unlock();
	return true;
}
